package com.sllp.C71S3TsllpShop.web.back;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.sllp.C71S3TsllpShop.bean.Business;
import com.sllp.C71S3TsllpShop.bean.Favorite;
import com.sllp.C71S3TsllpShop.bean.OrdersItem;
import com.sllp.C71S3TsllpShop.bean.Product;
import com.sllp.C71S3TsllpShop.bean.Specs;
import com.sllp.C71S3TsllpShop.biz.impl.BigTypeBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.OrdersItemBizImpl;
import com.sllp.C71S3TsllpShop.biz.impl.SmallTypeBizImpl;

@Controller
public class BackOrderFindAction {

	@Resource
	private OrdersItemBizImpl oibi;
	@Resource
	private BigTypeBizImpl btbi;
	@Resource
	private SmallTypeBizImpl stbi;
	
	@GetMapping("back/toOrderFind.do")
	public String toOrderFind(@SessionAttribute(name="backLoginBusiness",required=false) Business business,Model m) {
		Product product = new Product();
		product.setBusiness(business.getId());
		OrdersItem ordersItem = new OrdersItem();
		m.addAttribute("ordersItemList", oibi.selectOrdersItem(product,null,null,ordersItem ,1,2));
		m.addAttribute("bigTypeList", btbi.findAll());
		return "back/order-find::orderFind";
	}
	
	@PostMapping("back/findSmallType.do")
	public String findSmallType(int bigType,Model m) {
		m.addAttribute("smallTypeList", stbi.findByParent(bigType));
		return "back/order-find::smallTypeSelect";
	}
	
	@PostMapping("back/ordersItemShowToPage.do")
	public String ordersItemShowToPage(@RequestParam(required=false) String productName,
			@RequestParam(required=false) Integer smallType,
			@RequestParam(required=false) BigDecimal prePrice,
			@RequestParam(required=false) BigDecimal nextPrice,
			@RequestParam(required=false) Integer ordersItemState,
			@SessionAttribute(name="backLoginBusiness",required=false) Business business,int page,Model m) {
		
		Product product = new Product();
		product.setBusiness(business.getId());
		if(productName!=null && productName != "") {
			product.setName("%"+productName+"%");
		}
		if(smallType !=null && smallType != 0) {
			product.setType(smallType);
		}
		OrdersItem ordersItem = new OrdersItem();
		if(ordersItemState !=null && ordersItemState != 0) {
			ordersItem.setState(ordersItemState);
		}
		m.addAttribute("ordersItemList", oibi.selectOrdersItem(product,null,null,ordersItem ,page,2));
		
		return "back/order-find::ordersItemListTable";
	}
	
	@PostMapping("back/ordersItemFindByCondition.do")
	public String ordersItemSelectByCondition(@RequestParam(required=false) String productName,
			@RequestParam(required=false) Integer smallType,
			@RequestParam(required=false) BigDecimal prePrice,
			@RequestParam(required=false) BigDecimal nextPrice,
			@RequestParam(required=false) Integer ordersItemState,
			@SessionAttribute(name="backLoginBusiness",required=false) Business business,Model m) {
		
		Product product = new Product();
		product.setBusiness(business.getId());
		if(productName!=null && productName != "") {
			product.setName("%"+productName+"%");
		}
		if(smallType !=null && smallType != 0) {
			product.setType(smallType);
		}
		OrdersItem ordersItem = new OrdersItem();
		if(ordersItemState !=null && ordersItemState != 0) {
			ordersItem.setState(ordersItemState);
		}
		m.addAttribute("ordersItemList", oibi.selectOrdersItem(product,prePrice,nextPrice,ordersItem ,1,2));
		
		return "back/order-find::ordersItemListTable";
	}
	
	@PostMapping("back/updateOrdersItemStateInOderFind.do")
	public String updateOrdersItemStateInOderFind(@RequestParam(required=false) String productName,
			@RequestParam(required=false) Integer smallType,
			@RequestParam(required=false) BigDecimal prePrice,
			@RequestParam(required=false) BigDecimal nextPrice,
			@RequestParam(required=false) Integer ordersItemState,
			int state,
			String id,
			@SessionAttribute(name="backLoginBusiness",required=false) Business business,Model m) {
		OrdersItem ordersItem = new OrdersItem();
		ordersItem.setId(id);
		if(state == 5) {
			state = 6;
		}else if(state == 2) {
			state = 3;
		}
		ordersItem.setState(state);
		oibi.updateOrdersItemState(ordersItem);
		
		Product product = new Product();
		product.setBusiness(business.getId());
		if(productName!=null && productName != "") {
			product.setName("%"+productName+"%");
		}
		if(smallType !=null && smallType != 0) {
			product.setType(smallType);
		}
		OrdersItem ordersItem1 = new OrdersItem();
		if(ordersItemState !=null && ordersItemState != 0) {
			ordersItem1.setState(ordersItemState);
		}
		m.addAttribute("ordersItemList", oibi.selectOrdersItem(product,prePrice,nextPrice,ordersItem1 ,1,2));
		return "back/order-find::ordersItemListTable";
	}
	
}
